<template>
<div>
    <div class="mine" @tap="gotoAdminDetail()">
      <img class="mine_photo" :src="photo"/><!--src="../../static/img/mine/touxiang.png"-->
<!--    v-if="avatarName.avatarUrl==undefined"   <image class="mine_photo" v-else :src="avatarName.avatarUrl" mode="scaleToFill" ></image>-->
      <div class="mine_detail">
        <div class="mine_name">
          {{ realname }}
        </div>
<!--        v-if="name" <div class="mine_name" v-else-if="avatarName.nickName ">-->
<!--          {{ avatarName.nickName }}-->
<!--        </div>-->
<!--        <div class="mine_name" v-else>-->
<!--        </div>-->
        <div class="mine_edit">查看并编辑个人资料</div>
      </div>
    </div>
    <div class="interval"></div>
    <div class="inputItem">
      <div class="inputItem_label">
        <span class="inputItem_label_must"></span>
        电站名称:
      </div>
      <div class="inputItem_inputWrapper">
        <uni-easyinput
            v-model="dianZhanMingCheng"
            :inputBorder="false"
            :clearable="true"
            placeholder="请输入电站名称"
            :styles="{disableColor:'transparent'}"><!--:clearable="false"-->
        </uni-easyinput>
      </div>
      <div class="submitWrapper">
        <div class="submit" @tap="search()">查询</div>
      </div>
    </div>
    <div class="segment">
      <uni-segmented-control :current="current" :values="items" @clickItem="onClickItem" styleType="text" activeColor="#D9261D"></uni-segmented-control>
      <div class="content" > <!-- v-show="current === 0" -->
        <div v-if="stationList.length>0">
          <station-list :list="stationList" :status="state"/><!-- title="我管理的电站" -->
          <uni-load-more :status="loadMoreStatus" :content-text="contentText"></uni-load-more>
        </div>
        <empty v-else text="没有任何搜索内容"/>
      </div>
    </div>

</div>
</template>

<script>
  import StationList from "@/components/StationList";
  import wxPromisify from "@/libs/wxPromisify";
  import LoginManager from "@/utils/LoginManager";
  import Empty from "@/components/Empty";
  // import log from "@/libs/log";

  export default {
    name: "index",
    components: {StationList,Empty},
    data() {
      return {
        realname:'',
        dianZhanMingCheng: '',
        stationList: [],
        items: ['全部', '未审核', '审核中','已审核', '未通过','草稿'],
        current: 0,
        state:'',
        loadMoreStatus: 'more',//'loading','more',
        contentText: {
          contentdown: '上拉加载更多',
          contentrefresh: '加载中',
          contentnomore: '没有更多'
        },
        photo:'',//业务员头像
        page: 1,//页码
        limit: 10 //每页几条
        // tableData:[
        //   {
        //     prodname: "电站名称",
        //     name: "发布人姓名",
        //     phone: "发布人联系方式",
        //     sub_date:'发布时间',
        //     level:'权重',
        //   },
        // ],
        // searchVal: '',
        // // 每页数据量
        // pageSize: 4,
        // // 当前页
        // pageCurrent: 1,
        // // 数据总量
        // total: 0,
        // loading: false
      }
    },
    methods: {
      onClickItem(e) {
        if (this.current != e.currentIndex) {
          this.current = e.currentIndex;
          this.state=this.items[this.current];
          console.log('this.state:',this.state);
          this.page=1;//每次点击切换状态 page置为1
          this.stationList=[];
          console.log("onClickItem");
          this.init();
        }
      },
      search(){//查询
        this.page=1;
        this.stationList=[];
        console.log("search");
        this.init();
      },
      async init() {//所有电站列表
        //展示loading
        this.loadMoreStatus = 'loading';
        let param = {
          state: this.state==='全部'?'':this.state,
          fblx: '电站出售',
          name: this.dianZhanMingCheng,
          page: this.page,//'1',
          limit: this.limit,//pagesize,
          token: this.$LoginManager.getToken(),
          jjrchose:'全部',//经纪人选项
        }
        console.log('param', param);
        if(this.page===1){
          this.stationList=[];
        }
        let res=await this.$Api.getProdDetail5(param)
        for (let item of res.data) {
          let imgurl="";
          if(item.cover_url){//设置了封面图片，根据图片id调接口查http地址
            let imgid=item.cover_url;
            let imgParam = {
              objectName: imgid,
              token: this.$LoginManager.getToken(),
            }
            let urlres=await this.$Api.getFileUrl1(imgParam)
            if(urlres.code==0){
              imgurl=urlres.data[imgid];
              // console.log('imgurl:',imgurl);
            }
          }
          this.stationList.push({
            picture: imgurl,//item.cover_url,//item.jfzp_url.split(',')[0],
            location: item.province+"-"+item.city,//item.area.replace(" ", "-"),
            info: '机组*' + item.sl + '     ' + item.rl + 'KW',//'机组*' + item.jzsl + '     ' + item.kw + 'KW',
            price: item.amount + '万',//item.price + '万',
            id: item.id,
            status: item.state,
            source:'adminMine',
            edit:true,
            prodname:item.prodname,
            cust:item.name+' '+item.phone,
            sub_date:item.sub_date?item.sub_date.replace("T"," "):item.sub_date,
          });
        }
        if (res.data.length === this.limit) {
          this.page = this.page + 1;
          this.loadMoreStatus = 'more';
        } else {
          this.loadMoreStatus = 'noMore';
        }
      },

      async gotoAdminDetail() {
        try {
          await this.fetchLoginInfo();
          await wxPromisify.redirectTo({
            url: `/pages/mine/mineDetail`
          })
        } catch (e) {
          // log.info(e);
          console.log(e);
        }
      },
      async fetchLoginInfo() {
        if(this.$LoginManager.getLoginType()==="admin"){//业务员
          let isLogin = this.$LoginManager.checkLoginStatus();
          // log.info("业务员isLogin=",isLogin);
          if (!isLogin) {
            await wxPromisify.navigateTo({
              url: '/pages/login/adminIndex'//业务员登录
            })
          }else{
            // log.info("之前登录过，校验是否过期");
            //console.log("之前登录过，校验是否过期");
            let {returnCode, resultData} = await LoginManager.appletsCheckToken(LoginManager.getToken());//校验token是否过期
            // log.info("returnCode=",returnCode);
            // log.info("resultData=",resultData);
            if(returnCode!="0"){//token已过期 不存在
              // log.info("appletsCheckToken校验token已过期");
              let {returnCode1, resultData1} = await this.$LoginManager.CheckLoginByCode();//以前登陆过重新生成token
              // console.log("returnCode1=",returnCode1);
              // console.log("resultData1=",resultData1);
              // log.info("CheckLoginByCode的returnCode1==",returnCode1);
              if(returnCode1!="0"){//returnCode等于0代表自动登录，不为0代表该openid在表中未查到，打开登录页面
                await wxPromisify.navigateTo({
                  url: '/pages/login/adminIndex'//业务员登录
                })
              }
            }
          }
        }else{ //else if(!this.$LoginManager.getLoginType()){//以前没登陆过跳转到登录页
          // log.info("open adminIndex");
          await wxPromisify.navigateTo({
            url: '/pages/login/adminIndex'
          })
        }
      },
    },
    onLoad() {
       this.fetchLoginInfo();
      // console.log('mine onLoad');
      let isLogin = this.$LoginManager.checkLoginStatus();
      console.log("isLogin=",isLogin);
      if (isLogin) {
        //if(LoginManager.getToken()){
        let params = {
          token: LoginManager.getToken(),
        }
        //获取客户姓名
        this.$Api.adminRedisInfo(params)
            .then((res) => {
              if(res!=undefined && res.code=='99'){//说明token已失效，需要重新登录
                this.stationList=[];
                this.realname ='请登录';
              }
              //console.log('res====', res);
              // log.info("adminRedisInfo");
              if(res.returnCode=='0'){//获取成功
                let data=res.resultData;
                this.realname = data.realname;//员工姓名;
                this.photo=data.photo!=null && data.photo!=""?data.photo:'../../static/img/mine/touxiang.png';//员工头像
              }
            });
        this.page=1;
        this.stationList=[];
        this.init();
      }else{
        this.stationList=[];
        this.realname ='请登录';
      }
    },
    // onShow() {
    //   console.log('mine onshow');
    //   let isLogin = this.$LoginManager.checkLoginStatus();
    //   console.log("isLogin=",isLogin);
    //   if (isLogin) {
    //   //if(LoginManager.getToken()){
    //     let params = {
    //       token: LoginManager.getToken(),
    //     }
    //     //获取客户姓名
    //     this.$Api.adminRedisInfo(params)
    //         .then((res) => {
    //           if(res!=undefined && res.code=='99'){//说明token已失效，需要重新登录
    //             this.stationList=[];
    //             this.realname ='请登录';
    //           }
    //           //console.log('res====', res);
    //           log.info("adminRedisInfo");
    //           if(res.returnCode=='0'){//获取成功
    //             let data=res.resultData;
    //             this.realname = data.realname;//员工姓名;
    //             this.photo=data.photo!=null && data.photo!=""?data.photo:'../../static/img/mine/touxiang.png';//员工头像
    //           }
    //         });
    //     this.page=1;
    //     this.stationList=[];
    //     this.init();
    //   }else{
    //     this.stationList=[];
    //     this.realname ='请登录';
    //   }
    //
    // },
    onReachBottom() {
      if (this.loadMoreStatus != 'noMore') {
        console.log('onReachBottom');
        this.init();
      }
    },

  }
</script>

<style scoped lang="scss">
.mine {
  margin-top: 40px;
  margin-left: 40px;
  margin-bottom: 24px;

  .mine_photo {
    width: 120px;
    height: 120px;
    border-radius: 50%;
    vertical-align: top;
  }

  .userinfo-avatar {
    overflow: hidden;
    width: 120px;
    height: 120px;
    border-radius: 50%;
  }

  .mine_detail {
    display: inline-block;
    margin-left: 48px;


    .mine_name {
      font-size: 40px;
      font-family: PingFangSC-Semibold, PingFang SC;
      font-weight: 600;
      color: #333333;
    }

    .mine_edit {
      margin-top: 26px;
      font-size: 24px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #999999;
    }
  }
}
.interval{
  width: 750px;
  height: 24px;
  background: #F5F5F5;
}

.inputItem_label_must {
  display: inline-block;
  width: 24px;
  color: red;
  margin-right: 5px;
}

.inputItem {
  display: flex;
  align-items: center;
  height: 100px;
  margin-left: 10px;
  margin-right: 10px;
  border-bottom: 1px solid #E6E6E6;

  .inputItem_label {
    display: inline-block;
    width: 170px;
    font-size: 32px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #333333;
  }

  .inputItem_inputWrapper {
    flex: 1;
    display: inline-block;
    margin-left: 8px;
    -webkit-text-fill-color: #333333;
  }
}

.submitWrapper {
  background: white;
  border-top: 24px solid #F5F5F5;
}

.submit {
  margin: 24px 60px;
  width: 100px;
  height: 70px;
  background: #D9261D;
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #FFFFFF;
}

//.uni-swiper-tab{
//  border-bottom: 1px solid #EEEEEE;
//}
//.active{
//  color: #343434;
//}
//.active .swiper-tab-line{
//  border-bottom: 6px solid #D9261D;//#5598E7;
//  width: 70px;
//  margin: auto;
//  border-top: 6px solid #D9261D;//#5598E7;
//  border-radius: 20px;
//}

.segment{
  margin-top: 20px;
}

.uni-group {
  display: flex;
  align-items: center;
}
</style>